<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>照片管理</title>
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css">
</head>
<body>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>
<script type="text/html" id="barDemo">
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<img id="i"/>
</body>
<script src="../../assets/libs/jquery/jquery-3.2.1.min.js"></script>
<script src="../../assets/libs/layui/layui.js"></script>
<script src="../../assets/js/my.js"></script>

<script>
    function show_img(t) {
        var t = $(t).find("img");
        //页面层
        layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['80%', '80%'], //宽高
            shadeClose: true, //开启遮罩关闭
            end: function (index, layero) {
                return false;
            },
            content: '<div style="text-align:center"><img src="' + $(t).attr('src') + '" /></div>'
        });
    }
    layui.use(['layer', 'jquery', 'table'], function () {
        var layer = layui.layer;
        var table = layui.table;
        var $ = layui.jquery;

        table.render({
            elem: '#test'
            , url: '/photoservlet/list'
            , toolbar: '#toolbarDemo'
            , title: '照片实例'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'pId', title: 'ID', width: 80, fixed: 'left', unresize: true, sort: true}
                , {field: 'pName', title: '照片名称', width: 120,}
                , {field: 'pPath', title: '照片路径', width: 80, sort: true}
                , {field: 'eName', title: '照片作者', width: 80, sort: true}
                , {field: 'pPath', title: '图片', sort: true, templet: function (d) {
                        return '<div onclick="show_img(this)" ><img src="../upload/' + d.pPath.substr(47) + '" alt="" width="50px" height="50px"></a></div>';
                    }}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]]
            , page: true
        })
        table.on('tool(test)', function (obj) {
            var data = obj.data;


            if (obj.event === 'detail') {
                var i = $('#i');
                i.src = data.pPath;
                layer.open({
                    type: 1,
                    title: data.pName + '   作者：' + data.eName,
                    content: $('#i')
                })
            } else if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                    $.post(host + '/photoservlet/removeById', {pId: data.pId,pPath:data.pPath}, function (r) {
                        if (r.code == 0) {
                            layer.msg('删除成功')
                        } else {
                            layer.msg('删除失败')
                        }
                    },'json')
                });
            }
        });
        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;
                case 'add':
                    layer.open({
                        type: 2,
                        content: ['photoAdd.html'],
                        area: ['600px', '400px'],
                        maxmin: true,
                        cancel: function (index) {
                            $(".layui-laypage-btn").click();//这里用于关闭Open时触发回调函数  刷新父页面数据  一定要引入Jquery
                        }
                    })
                    break;
            }
            ;
        });

    })

</script>
</html>